import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


class Request {
    public String aa;
    public String bb;
}

class Response {
    public boolean ok;
}

@WebServlet("/jsonParameter")
public class JsonParameterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 POST 请求中 body 的内容
        // body 格式的处理:  { "aa": 10, "bb":20}   (json)
        ObjectMapper objectMapper = new ObjectMapper();
        // 通过提供的类对象和body中的json数据构造成一个java对象 并 将json数据打印到服务器控制台上
        Request request = objectMapper.readValue(req.getInputStream(), Request.class);
        System.out.println("aa:" + request.aa);
        System.out.println("bb:" + request.bb);

        Response response = new Response();
        response.ok = true;
        // 把响应对象转成 json格式的字符串
        String respJson = objectMapper.writeValueAsString(response);
        resp.getWriter().write(respJson);
    }
}
